package com.fs.web.base.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @desc 日期时间工具类
 * @author TiidlerCJ
 */
public class DateUtils {
	public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String TIME = "2017-12";
	
	public static String formatDate(Date date, String pattern) {
    	if(pattern == null || "".equals(pattern)){
			//"yyyy-MM-dd HH:mm:ss"
    		pattern = DEFAULT_PATTERN;
    	}
	     java.text.DateFormat df = new SimpleDateFormat(pattern);
	     String s = df.format(date); 
	     return s;
	}
	
	public static String curDate(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(new Date());
    }

    public static String curDate() {
        return curDate(DEFAULT_PATTERN);
    }
    
    
    public static String startTime() {
        return curDate("yyyy-MM-dd 00:00:00");
    }
    
    public static String endTime() {
    	return curDate("yyyy-MM-dd 23:59:59");
    }
    
    public static Date parseDate(String dateStr, String pattern) {
    	if(pattern==null || "".equals(pattern)){
			//"yyyy-MM-dd HH:mm:ss"
    		pattern = DEFAULT_PATTERN;
    	}
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date ret = null;
        try {
            ret = dateFormat.parse(dateStr);
        } catch (Exception e) {
        	System.err.println(dateStr+"can't parseTo pattern Style!");
            e.printStackTrace();
        }
        return ret;
    }



    /**前一天*/
    public static String beforeDay(){
    	Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); 
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

	/**前n天或后n天*/
    public static String beforeDay(int n){
    	Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, n);
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

	 /**获取某月的前n月或后n月*/
    public static String mouth(String mouth,Integer number){
    	Date month = parseDate(mouth, "yyyyMM");
    	Calendar dar = Calendar.getInstance();
		dar.setTime(month);
		dar.add(Calendar.MONTH, number);
		return DateUtils.formatDate(dar.getTime(), "yyyyMM");
    }

	 /**获取当前时间的前n秒或后n秒*/
    public static String second(Integer number){
    	Calendar dar = Calendar.getInstance();
		dar.setTime(new Date());
		dar.add(Calendar.SECOND, number);
		return DateUtils.formatDate(dar.getTime(), "yyyy-MM-dd HH:mm:ss");
    }

	/**
	 * 获取一段时间之内的年份(例:201705-201801)时间格式为:yyyyMM
	 * @param startMonth 格式yyyyMM
	 * @param endMonth	格式yyyyMM
	 * @return List<String> 返回时间格式yyyyMM 注意：若开始时间大于或等于结束时间那么只会返回开始时间
	 */
	public static List<String> intervalYear(String startMonth,String endMonth,String pattern) {
		List<String> timeList = new ArrayList<>();
		if(parseDate(startMonth, pattern).getTime() >= parseDate(endMonth, pattern).getTime()){
			timeList.add(startMonth);
		}else{
			Calendar start = Calendar.getInstance();
			Calendar end = Calendar.getInstance();

			start.setTime(parseDate(startMonth, pattern));
			start.set(start.get(Calendar.YEAR),start.get(Calendar.MONTH),1);

			end.setTime(parseDate(endMonth, pattern));
			end.set(end.get(Calendar.YEAR),start.get(Calendar.MONTH),2);

			Calendar curr = start;
			while (curr.before(end)) {
				timeList.add(formatDate(curr.getTime(), "yyyy"));
				curr.add(Calendar.YEAR, 1);
			}
		}
		return timeList;
	}

    /**
     * 获取一段时间之内的月份(例:201705-201801)时间格式为:yyyyMM
     * @param startMonth 格式yyyyMM
     * @param endMonth	格式yyyyMM
     * @return List<String> 返回时间格式yyyyMM 注意：若开始时间大于或等于结束时间那么只会返回开始时间
     */
    public static List<String> intervalMonth(String startMonth,String endMonth) {
    	List<String> timeList = new ArrayList<>();
    	if(parseDate(startMonth, "yyyyMM").getTime() >= parseDate(endMonth, "yyyyMM").getTime()){
    		timeList.add(startMonth);
    	}else{
    		Calendar start = Calendar.getInstance();
    		Calendar end = Calendar.getInstance();
    		
    		start.setTime(parseDate(startMonth, "yyyyMM"));
    		start.set(start.get(Calendar.YEAR),start.get(Calendar.MONTH),1);
    		
    		end.setTime(parseDate(endMonth, "yyyyMM"));
    		end.set(end.get(Calendar.YEAR),end.get(Calendar.MONTH),2);
    		
    		Calendar curr = start;  
    		while (curr.before(end)) {
    			timeList.add(formatDate(curr.getTime(), "yyyyMM"));
    			curr.add(Calendar.MONTH, 1);
			}
    	}
		return timeList;
	}

	/**
	 * 获取一段日期之内的月份(例:2017-05-01~2018-01-01)时间格式为:yyyy-MM-dd
	 * @param startMonth 格式yyyy-MM-dd
	 * @param endMonth	格式yyyy-MM-dd
	 * @return List<String> 返回时间格式yyyyMM 注意：若开始时间大于或等于结束时间那么只会返回开始时间
	 */
	public static List<String> intervalMonths(String startMonth,String endMonth) {
		List<String> timeList = new ArrayList<>();
		if(parseDate(startMonth, "yyyy-MM-dd").getTime() >= parseDate(endMonth, "yyyy-MM-dd").getTime()){
			timeList.add(startMonth.replaceAll("-","").substring(0,6));
		}else{
			Calendar start = Calendar.getInstance();
			Calendar end = Calendar.getInstance();

			start.setTime(parseDate(startMonth, "yyyy-MM-dd"));
			start.set(start.get(Calendar.YEAR),start.get(Calendar.MONTH),1);

			end.setTime(parseDate(endMonth, "yyyy-MM-dd"));
			end.set(end.get(Calendar.YEAR),end.get(Calendar.MONTH),2);

			Calendar curr = start;
			while (curr.before(end)) {
				timeList.add(formatDate(curr.getTime(), "yyyyMM"));
				curr.add(Calendar.MONTH, 1);
			}
		}
		return timeList;
	}

    /**
     * 获取一段日期之内的月份(例:2017-05~2018-01)时间格式为:yyyy-MM
     * @param startMonth 格式yyyy-MM
     * @param endMonth	格式yyyy-MM
     * @return List<String> 返回时间格式yyyy-MM 注意：若开始时间大于或等于结束时间那么只会返回开始时间
     */
    public static List<String> intervalMonth1(String startMonth,String endMonth) {
        List<String> timeList = new ArrayList<>();
        if(parseDate(startMonth, "yyyy-MM").getTime() >= parseDate(endMonth, "yyyy-MM").getTime()){
            timeList.add(startMonth);
        }else{
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();

            start.setTime(parseDate(startMonth, "yyyy-MM"));
            start.set(start.get(Calendar.YEAR),start.get(Calendar.MONTH),1);

            end.setTime(parseDate(endMonth, "yyyy-MM"));
            end.set(end.get(Calendar.YEAR),end.get(Calendar.MONTH),2);

            Calendar curr = start;
            while (curr.before(end)) {
                timeList.add(formatDate(curr.getTime(), "yyyy-MM"));
                curr.add(Calendar.MONTH, 1);
            }
        }
        return timeList;
    }

    /**
     * 获取一段时间之内的日期(例:2017-05-01~2018-01-03)时间格式为:yyyy-MM-dd
     * @param startDay 格式yyyy-MM-dd
     * @param endDay	格式yyyy-MM-dd
     * @return List<String> 返回时间格式yyyy-MM-dd
     */
    public static List<String> intervalDay(String startDay,String endDay) {
    	List<String> timeList = new ArrayList<>();
    	if(parseDate(startDay, "yyyy-MM-dd").getTime() > parseDate(endDay, "yyyy-MM-dd").getTime()){
    		timeList.add(curDate("yyyy-MM-dd"));
    	}else{
    		Calendar start = Calendar.getInstance();
    		Calendar end = Calendar.getInstance();
    		
    		start.setTime(parseDate(startDay, "yyyy-MM-dd"));
    		start.set(start.get(Calendar.YEAR),start.get(Calendar.MONTH),start.get(Calendar.DAY_OF_MONTH));
    		
    		end.setTime(parseDate(endDay, "yyyy-MM-dd"));
    		end.set(end.get(Calendar.YEAR),end.get(Calendar.MONTH),end.get(Calendar.DAY_OF_MONTH));
    		
    		Calendar curr = start;  
    		while (curr.before(end)) {
    			timeList.add(formatDate(curr.getTime(), "yyyy-MM-dd"));
    			curr.add(Calendar.DAY_OF_MONTH, 1);
    		}
    		timeList.add(endDay);
    	}
    	return timeList;
    }

}
